Python Variables and Data Types Explained with Examples for Beginners
Introduction
Table of contents
What is a Variable?
A variable is a name used to store data in a program. Data can be number, text, or object. You can think of it as a container that holds a value and whenever we need that data we need to call that container by its name.Example
Python Code
name = "Haarsh"
age = 20
In this example, name stores text, and age stores a number.
Rules for Declaring Variables
Uses of Variable Declaration in Python
- Store Data :Variables keep values in memory.
Python CodeYou can use age later in the program whenever you need.age = 21 - Reuse Values : Instead of writing the same value repeatedly:
- Perform Calculations :Variables help in mathematical operations.
Python Codea = 5 b = 3 sum = a + b print(sum) - Store User Input
- Update Values Dynamically :Variables can change during execution.
Python Code
pi = 3.14
area = pi * r * r
Python Code
name = input("Enter your name: ")
print(name)
Python Code
score = 10
score = score + 5
Now score becomes 15.
Common Mistakes in Python Variables
- Using a Variable Before Declaring It
Python Codeprint(name) name = "Rahul"Python Codename = "Rahul" print(name) - Forgetting Quotes for Strings
Python CodePython thinks Rahul is another variable.name = Rahul
Python Codename = "Rahul" - Using Python Keywords as Variable Names
Python CodeExplainnation:- class is a Python keyword.class = 10
Python Codeclass_name = 10 - Starting Variable Name with a Number
Python Code1name = "Ali"Python Codename1 = "Ali" - Case Sensitivity ConfusionPython treats uppercase and lowercase differently.
Python Codeage = 20 print(Age)Python Codeage = 20 print(age) - Using Spaces in Variable Names
Python Codestudent name = "Amit"❌Python Codestudent_name = "Amit"✅ - Using Special Characters
❌ We cannot use special characters like &,*,-,=,@ etc.
Python Code✅ We only use underscroreuser-name = "Sam"Python Codeuser_name = "Sam" Choosing Poor Variable Names :
This Type of variable is hard to understand by reader. Suppose you are a developer and you take variable name "X" for name. When code tester read the code they are unable to know what value does variable "X" hold untill they excute the code side by side. Instead of "X" if developer take variable is "name" than tester easily understand that the use of that variablePython Codea = 50000 ❌ Reader will confused What is variable "a"Python Codesalary = 50000 ✅ Reader will easily understand about variable salaryOverwriting Important Variables
Python CodeNow x is no longer a number.x = 10 x = "hello"Confusing in EqualTo "=" and Double EqualTo "=="
= → assignment
= = → comparison
Python Codeif x = 5: if x == 5:
What are Data Types?
Data types tell Python what kind of value is stored in a variable. It represents what kind of operation can be done on a particular data. Different data types are used for different kinds of data.Common Python Data Types
Data Type | Example | Description |
| int | 5 | For Whole numbers |
| float | 5.5 | For Decimal numbers |
| str | "Python" | For Text |
| bool | True | For Boolean values |
Here is the some example
Python Code
x = 10
y = 3.5
message = "Hello, Python"
is_student = True
Here:
x is an integer.y is a float.
message is a string.
is_student is a boolean.
Checking the Data Type
You can check the data type of a variable using the `type()` function.
Python Code
num = 5
print(type(num))
Python Code Output
class 'int'
Taking Input from the User
Python allows you to take input from the user using the `input()` function. But remember, input is always taken as text by default.Here is the Example:
Python Code
name = input("Enter your name: ")
print("Hello", name)
Python Code Output
Enter your name: Haarsh
Hello Haarsh
Type Conversion
If you want to perform calculations, you must convert text input into a number using `int()` or `float()`. Example
Python Code
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Sum:", num1 + num2)
This program takes two numbers from the user and adds them.
Python Code Output
Enter first number: 5
Enter second number: 3
Sum: 8
Full Example
Python Code
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name)
print("Next year, you will be", age + 1)
Python Code Output
Enter your name: Ravi
Enter your age: 20
Hello Ravi
Next year, you will be 21
Explanation